package com.soulkun.smarthome.ui.main.personal.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.amap.api.maps.model.LatLng
import com.soulkun.smarthome.logic.dto.FamilyUserRoleDTO
import com.soulkun.smarthome.logic.model.FamilyInvitationModel
import com.soulkun.smarthome.logic.model.FamilyModel
import com.soulkun.smarthome.logic.model.RoomModel
import com.soulkun.smarthome.logic.repository.ApiRepository
import com.soulkun.smarthome.logic.vo.FamilyMemberVO
import com.soulkun.smarthome.logic.vo.RoomVO
import soulkun.library.mvvm.adapter.clearAndAddAll
import soulkun.library.utils.toast

class AppMainPersonalFamilySettingViewModel : ViewModel() {

    val familyIdLiveData = MutableLiveData<Long>()
    val familyLocationLiveData = MutableLiveData<LatLng>()
    val selectFamilyUserLiveData = MutableLiveData<FamilyMemberVO>()

    val familyNameLiveData = MutableLiveData<String>()
    val familyRoomCountLiveData = MutableLiveData<String>()
    val familyRoomList = ArrayList<RoomVO>()
    val familyMemberList = ObservableArrayList<FamilyMemberVO>()
    val familyInvitationList = ObservableArrayList<FamilyInvitationModel>()

    fun getFamilyDetailInfo() {
        ApiRepository.getFamilyDetailInfo(
            this,
            familyModel = FamilyModel(id = familyIdLiveData.value)
        ) {
            familyNameLiveData.value = it.data.family.name!!
            familyLocationLiveData.value =
                LatLng(it.data.family.latitude!!, it.data.family.longitude!!)
            familyRoomList.clear()
            familyRoomList.addAll(it.data.roomList.map { roomModel ->
                RoomVO(
                    roomModel.id!!,
                    ObservableField(roomModel.name),
                    roomModel.familyId!!
                )
            })
            familyRoomCountLiveData.value = "${it.data.roomList.size}个房间"
            familyMemberList.clearAndAddAll(it.data.familyUserList.sortedBy { familyUserDTO ->  familyUserDTO.role.id }.map { familyUserDTO ->
                FamilyMemberVO(
                    familyIdLiveData.value!!,
                    familyUserDTO.user.id!!,
                    familyUserDTO.user.userName!!,
                    familyUserDTO.user.nickName!!,
                    ObservableField(familyUserDTO.role)
                )
            })
            familyInvitationList.clearAndAddAll(it.data.familyInvitationList)
        }
    }

    fun modifyFamilyName(familyName: String, onComplete: () -> Unit) =
        ApiRepository.modifyFamilyName(this, FamilyModel(familyIdLiveData.value, familyName)) {
            it.statusMessage.toast()
            familyNameLiveData.value = familyName
            onComplete()
        }

    fun deleteFamily() = ApiRepository.deleteFamily(FamilyModel(id = familyIdLiveData.value))

    fun setFamilyUserRole(roleId: Long) =
        ApiRepository.setFamilyUserRole(
            FamilyUserRoleDTO(
                familyIdLiveData.value!!,
                selectFamilyUserLiveData.value!!.userId,
                roleId
            )
        )

    fun deleteMember() = ApiRepository.deleteFamilyUser(
        FamilyUserRoleDTO(familyIdLiveData.value!!, selectFamilyUserLiveData.value!!.userId)
    )

    fun addMember() = ApiRepository.generateFamilyInvitation(FamilyModel(familyIdLiveData.value))

}